<template>
	<view>
		<!-- 弹窗授权 -->
		<uv-popup ref="popup" mode="bottom" round="20" :close-on-click-overlay="false">
			<view style="width: 800rpx;">
				<view style="margin: 60rpx 40rpx 40rpx 40rpx;">
					<uv-row>
						<uv-col span="1">
							<uv-image src="/static/QRCodeRxLogo.png" shape="circle" width="50rpx" height="50rpx"></uv-image>
						</uv-col>
						<uv-col span="9">
							<uv-text :bold="true" text="克尚处方 申请"></uv-text>
						</uv-col>
					</uv-row>
					<view style="margin-top: 40rpx;"><uv-text :bold="true" size="18" text="获取您的昵称、头像、手机号"></uv-text></view>
					<uv-line length="90%" customStyle="text-align: center;margin-top: 30rpx;margin-bottom: 30rpx;"></uv-line>
					<uv-row>
						<uv-col span="1">
							<uv-image src="/static/QRCodeRxLogo.png" width="65rpx" height="65rpx"></uv-image>
						</uv-col>
						<uv-col span="8.9">
							<view style="margin-left: 20rpx;">
								<uv-text text="获取微信ID"></uv-text>
								<uv-text type="info" text="微信个人信息"></uv-text>
							</view>
						</uv-col>
						<uv-col span="0.1">
							<view style="margin-left: auto;"><uv-icon name="checkmark" color="#2c9c3c" size="28"></uv-icon></view>
						</uv-col>
					</uv-row>
					<uv-line length="90%" customStyle="text-align: center;margin-top: 30rpx;margin-bottom: 50rpx;"></uv-line>
					<uv-row>
						<uv-col span="5">
							<view style="width: 70%;margin-left: auto;margin-right: auto;">
								<uv-button :hairline="false" text="拒绝" customTextStyle="color: #2c9c3c;font-size: 28rpx" @click="RxClose"></uv-button>
								</view>
						</uv-col>
						<uv-col span="5">
							<view style="width: 70%;margin-left: auto;margin-right: auto;">
								<uv-button :hairline="false" text="允许" color="#2c9c3c" customTextStyle="color: #ffffff;font-size: 28rpx" open-type="getPhoneNumber" @getphonenumber="Permit"></uv-button>
								</view>
						</uv-col>
					</uv-row>
				</view>
			</view>
		</uv-popup>
	</view>
</template>

<script>
	export default {
		name:"RxPopup",
		data() {
			return {
				
			};
		},
		methods: {
			RxOpen(){
				this.$refs.popup.open();
			},
			RxClose(){
				this.$refs.popup.close();
			},
			Permit(e) {
				this.RxClose()
				uni.showToast({ title: '检查登录中', icon: 'loading', duration: 60000 })
				console.log("手机号回调: ", e)
				this.GetWeChatUserInfo()
			},
			GetWeChatUserInfo() {
				var _this = this
				
				uni.login({
				    provider: 'weixin',
				    success: function (loginRes) {
						var userinfo = {
							types: _this.$store.state.userinfo.types,
							code: loginRes.code
						}
						
						_this.$api.get_or_create_users(userinfo).then((res)=> {
							userinfo = _this.$store.state.userinfo
							userinfo.userId = res.data.userId
							userinfo.qrcode = res.data.qrcode
							userinfo.name = res.data.name
							userinfo.mobile = res.data.mobile
							_this.$store.dispatch('set_userinfo', userinfo)
							_this.SkipPage(userinfo)
						})
				    }
				});

			},
			SkipPage(data) {
				if (data.qrcode === 1) {
					uni.hideToast()
					uni.navigateTo({
					    url:'/pages/firstQr/firstQr'
					});
				} else {
					
					var _this = this
					_this.$api.get_user_info({ userId: data.userId, types: data.types }).then((res) => {
						data.name = res.data.name
						data.mobile = res.data.mobile
						data.clinic = res.data.clinic
						data.clinic_data_all = res.data.clinic_data_all
						data.clinic_id_list = data.clinic_data_all.map(item => item._id)	// 使用map方法提取_id字段  
						data.self = res.data.self
						_this.$store.dispatch('set_userinfo', data)
						
						var pages = '/pages/patient/patient'
						if (data.types === 1) {
							pages = '/pages/medic/medic'
						} else if (data.types === 0) {
							pages = '/pages/nurse/nurse'
						} else if (data.types === 3) {
							pages = '/pages/visit/visit'
						}
						uni.hideToast()
						uni.reLaunch({
						    url: pages
						});
					}).catch((err) => {
						console.log(err)
						uni.showToast({ title: '服务错误', icon: 'error', duration: 3000 })
					})
					
				}
				
			}
		}
	}
</script>

<style>

</style>